package cn.jnetty.remoting.platform;
import io.netty.channel.MultithreadEventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.concurrent.ThreadFactory;

/**
 * @author 猪哥亮
 * @email 1030681978@qq.com
 * @date 2020/4/2 16:16
 * 描述：
 */
public class JnettyPlatform {
    public boolean isEpoll(){
        return Epoll.isAvailable();
    }
    public MultithreadEventLoopGroup getJnettyMultithreadEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
        return isEpoll() ? linuxLoopGroup(nThreads,threadFactory) : winLoopGroup(nThreads,threadFactory);
    }

    public Class getJnettyServerSocketChannel() {
        return isEpoll() ? linuxChannel() : winChannel();
    }
    private Class winChannel() {
        return NioServerSocketChannel.class;
    }
    private Class linuxChannel() {
        return EpollServerSocketChannel.class;
    }
    private NioEventLoopGroup winLoopGroup(int nThreads, ThreadFactory threadFactory){
        if(threadFactory != null){
            return new NioEventLoopGroup(nThreads,threadFactory);
        }
        return new NioEventLoopGroup(nThreads);
    }
    private EpollEventLoopGroup linuxLoopGroup(int nThreads, ThreadFactory threadFactory){
        if(threadFactory != null){
            return new EpollEventLoopGroup(nThreads,threadFactory);
        }
        return new EpollEventLoopGroup(nThreads);
    }
}
